home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / COMMS / C101.ZIP / UUPC11XS.ZIP / MAIL / MAILSEND.C < prev    next >
C/C++ Source or Header  |  1992-11-21  |  35KB  |  993 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    m a i l s e n d . c                                             */
  3. /*                                                                    */
  4. /*    Subroutines for sending mail for UUPC/extended                  */
  5. /*                                                                    */
  6. /*    Changes copyright 1990, Andrew H. Derbyshire                    */
  7. /*                                                                    */
  8. /*    Change History:                                                 */
  9. /*                                                                    */
  10. /*    15 Sep 90   - Create from maillib.c                         ahd */
  11. /*--------------------------------------------------------------------*/
  12.  
  13. #include <ctype.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <process.h>
  20.  
  21. #include "arpadate.h"
  22. #include "expath.h"
  23. #include "lib.h"
  24. #include "hlib.h"
  25. #include "mlib.h"
  26. #include "alias.h"
  27. #include "mail.h"
  28. #include "maillib.h"
  29. #include "mailblib.h"
  30. #include "mailsend.h"
  31. #include "safeio.h"
  32. #include "address.h"
  33.  
  34. #define  INDENT "> "
  35.  
  36. /*--------------------------------------------------------------------*/
  37. /*                     Local function prototypes                      */
  38. /*--------------------------------------------------------------------*/
  39.  
  40.  static char *ExplodeAlias(char *header ,
  41.                       const char *alias,
  42.                       FILE *stream,
  43.                       const boolean resent);
  44.  
  45.  
  46.  static void PutHead( const char *label,
  47.                       const char *operand,
  48.                       FILE *stream,
  49.                       const boolean resent);
  50.  
  51.  static boolean Append_Signature(FILE *mailbag,
  52.                      const boolean alternate);
  53.  
  54.  static void Prompt_Input( char *tmailbag,
  55.                           FILE *fmailbag,
  56.                            char *subject,
  57.                           const int current);
  58.  
  59.  static boolean Subcommand( char *buf,
  60.                            FILE *fmailbag,
  61.                            char *tmailbag,
  62.                            char *subject,
  63.                            const int current_msg);
  64.  
  65.  static void CopyOut( const char* input);
  66.  
  67.  static void filter( char *tmailbag, char *command);
  68.  
  69.  static char *GetString( char *input);
  70.  
  71. currentfile();                /* Define current file for panic()     */
  72.  
  73. /*--------------------------------------------------------------------*/
  74. /*    E x p l o d e A l i a s                                         */
  75. /*                                                                    */
  76. /*    Resolves an alias, exploding it into a list if needed.          */
  77. /*--------------------------------------------------------------------*/
  78.  
  79.  static char *ExplodeAlias(char *header ,
  80.                       const char *alias,
  81.                       FILE *stream,
  82.                       const boolean resent)
  83. {
  84.    char *fullname;
  85.    char buffer[LSIZE];
  86.  
  87.    if ((alias == NULL) || (strlen(alias) == 0))
  88.    {
  89.       printmsg(0,"ExplodeAlias: NULL or empty string for argument");
  90.       panic();
  91.    }
  92.  
  93.    fullname = AliasByNick(alias);
  94.  
  95.    printmsg(4,"Processing alias '%s', result '%s'", alias,
  96.       (fullname == NULL) ? alias : fullname);
  97.  
  98.    if (fullname == NULL)            /* No alias found for user?     */
  99.    {                                /* No --> Try node lookup       */
  100.       char user[MAXADDR];
  101.       char node[MAXADDR];
  102.       char path[MAXADDR];
  103.       char bucket[MAXADDR];
  104.  
  105.       ExtractAddress(bucket, (char *) alias, FALSE);
  106.       user_at_node(bucket, path, node, user);
  107.       fullname = AliasByAddr( node, user);
  108.  
  109.       if (fullname == NULL)         /* Did we come up empty?         */
  110.       {
  111.          char *hisuser, *hisnode;
  112.  
  113.          hisuser = strtok( bucket, "@");
  114.          hisnode = strtok( NULL, "@");
  115.          if ((*bucket != '@') &&
  116.              equal(hisuser, user ) && (hisnode != NULL) &&
  117.              equal(hisnode, node ) && (strchr( node, '.') == NULL))
  118.          {
  119.             if (equal(hisnode, E_nodename))
  120.                strcpy(node, E_fdomain);
  121.             else {
  122.                strcat(node,".");
  123.                strcat(node,E_localdomain);
  124.             }
  125.  
  126.             ExtractAddress(path, (char *) alias, TRUE);
  127.             if (strlen( path ) == 0)
  128.                sprintf(buffer,"%s@%s", hisuser, node );
  129.             else
  130.                sprintf(buffer,"\"%s\" <%s@%s>", path, hisuser, node);
  131.             fullname = buffer;
  132.          }
  133.          else
  134.             fullname = (char *) alias; /* Use original information      */
  135.       }
  136.    }
  137.    else {
  138.       ExtractAddress(buffer,fullname,TRUE);
  139.       if (strlen(buffer) == 0)      /* A list of users?              */
  140.       {                             /* Yes --> Do recursive call     */
  141.          char *current = buffer;    /* Current token being processed */
  142.          char *next = NULL;         /* Next token to process         */
  143.  
  144.          strcpy(buffer,fullname);
  145.  
  146.          do {
  147.             current = strtok(current,",\t "); /* Get next alias to process */
  148.             next    = strtok(NULL,"");    /* Also save rest of list        */
  149.             header  = ExplodeAlias( header , current, stream, resent);
  150.                                           /* Get alias, including sub-list */
  151.             current  = next;
  152.          } while ( next != NULL );        /* Until no more tokens exist    */
  153.  
  154.          return header;                   /* Have written header, return   */
  155.  
  156.       } /* if */
  157.    } /* else */
  158.  
  159.    if (strpbrk(fullname,"!@") == nil(char))
  160.    {
  161.       sprintf(buffer,"%s@%s", fullname , E_fdomain);
  162.                               /* Local address                    */
  163.       fullname = buffer;      /* Write out the formatted address  */
  164.    }
  165.  
  166.    PutHead(header, fullname, stream, resent);
  167.                               /* Remote address                   */
  168.    return "";                 /* Make header empty string for
  169.                                  next caller                      */
  170. } /* ExplodeAlias */
  171.  
  172. /*--------------------------------------------------------------------*/
  173. /*    A p p e n d _ S i g n a t u r e                                 */
  174. /*                                                                    */
  175. /*    Append the signature file to the specified mailbag file         */
  176. /*                                                                    */
  177. /*    [Broke this code out from Send_Mail to support the ~a mail      */
  178. /*    subcommand]                                                     */
  179. /*                                                                    */
  180. /*    Returns:  0 on success, 1 if signature file not found           */
  181. /*--------------------------------------------------------------------*/
  182.  
  183. static boolean Append_Signature(FILE *mailbag_fp ,
  184.                      const boolean alternate)
  185. {
  186.    FILE *sigfp;
  187.    char *sig;
  188.    char sigfile[FILENAME_MAX];
  189.    char buf[BUFSIZ];
  190.  
  191.    sig = alternate ? E_altsignature : E_signature;
  192.  
  193.    if(sig != nil(char)) {
  194.       mkfilename(sigfile, E_homedir, sig);
  195.       printmsg(4, "Append_Signature: signature file %s", sigfile);
  196.       if ((sigfp = FOPEN(sigfile, "r", TEXT)) != nil(FILE)) {
  197.          fputs("-- \n", mailbag_fp);
  198.          while (fgets(buf, BUFSIZ, sigfp) != nil(char))
  199.             fputs(buf, mailbag_fp);
  200.          fclose(sigfp);
  201.          return(0);
  202.       }
  203.       else {
  204.          printmsg(0, "Signature file \"%s\" doesn't exist!\n", sigfile);
  205.          return(1);
  206.       }
  207.    }
  208.    return(0);
  209. }  /* Append_Signature */
  210.  
  211. /*--------------------------------------------------------------------*/
  212. /*    S e n d _ M a i l                                               */
  213. /*                                                                    */
  214. /*    Send text in a mailbag file to address(es) specified by line.   */
  215. /*--------------------------------------------------------------------*/
  216.  
  217. boolean Send_Mail(FILE *datain,
  218.                int argc,
  219.                char *argv[],
  220.                char *subject,
  221.                const boolean resent)
  222. {
  223.    int argx = 0;
  224.    char buf[LSIZE];
  225.    char *header    = "To:";
  226.    char *CcHeader  = "Cc:";
  227.    char *BccHeader = "Bcc:";
  228.    char *pipename  = mktempname(NULL, "TMP");
  229.    FILE *stream = FOPEN(pipename , "w", TEXT);
  230.    int status;
  231.  
  232. /*--------------------------------------------------------------------*/
  233. /*                     Verify our workfile opened                     */
  234. /*--------------------------------------------------------------------*/
  235.  
  236.    if ( stream == NULL )
  237.    {
  238.       printerr(pipename);
  239.       free(pipename);
  240.       return FALSE;
  241.    }
  242.  
  243. /*--------------------------------------------------------------------*/
  244. /*    Add the boilerplate the front:                                  */
  245. /*                                                                    */
  246. /*       Date, From, Organization, and Reply-To                       */
  247. /*--------------------------------------------------------------------*/
  248.  
  249.    PutHead("Date:", arpadate() , stream, resent);
  250.  
  251.    if (bflag[F_BANG])
  252.       sprintf(buf, "(%s) %s!%s", E_name, E_fdomain, E_mailbox );
  253.    else
  254.       sprintf(buf, "\"%s\" <%s@%s>", E_name, E_mailbox, E_fdomain );
  255.    PutHead("From:", buf, stream , resent);
  256.  
  257.    if (E_organization != NULL )
  258.       PutHead("Organization:", E_organization, stream, resent );
  259.  
  260.    if (E_replyto != NULL )
  261.    {
  262.       if (strpbrk(E_replyto,"!@") == nil(char))
  263.          sprintf(buf,"\"%s\" <%s@%s>", E_name, E_replyto , E_fdomain);
  264.       else
  265.          sprintf(buf,"\"%s\" <%s>", E_name, E_replyto);
  266.       PutHead("Reply-To:", buf, stream, resent );
  267.    }
  268.  
  269. /*--------------------------------------------------------------------*/
  270. /*                      Write the addressees out                      */
  271. /*--------------------------------------------------------------------*/
  272.  
  273.    for (argx = 0 ; argx < argc; argx++ )
  274.    {
  275.       if (equal(argv[argx],"-c"))
  276.       {
  277.          header = CcHeader;
  278.          CcHeader = "";
  279.       } /* if */
  280.       else if (equal(argv[argx],"-b"))
  281.       {
  282.          header = BccHeader;
  283.          CcHeader = BccHeader = "";
  284.       } /* if else */
  285.       else
  286.          header = ExplodeAlias( header , argv[argx], stream, resent);
  287.    } /* for */
  288.  
  289. /*--------------------------------------------------------------------*/
  290. /*                  Prompt for carbon copies, if any                  */
  291. /*--------------------------------------------------------------------*/
  292.  
  293.    if ( bflag[F_ASKCC] && Is_Console(stdin) &&
  294.         Console_fgets(buf,LSIZE,"Cc: "))
  295.    {
  296.       char *current = buf;
  297.       header = CcHeader;
  298.       CcHeader = "";
  299.       printmsg(4,"CC buffer: %s",current);
  300.  
  301.       while ((current != NULL) &&
  302.              (current = strtok(current,",\t\n ")) != NULL)
  303.       {
  304.          char *next  =  strtok(NULL,"");
  305.          if (equal(current,"-b"))
  306.          {
  307.             header = BccHeader;
  308.             CcHeader = BccHeader = "";
  309.          } /* if */
  310.          else
  311.             header = ExplodeAlias( header, current, stream, resent);
  312.          current = next;
  313.       } /* while */
  314.    }  /* If Console_fgets() */
  315.  
  316. /*--------------------------------------------------------------------*/
  317. /*                     Handle the subject, if any                     */
  318. /*--------------------------------------------------------------------*/
  319.  
  320.    if (subject != NULL)
  321.       PutHead("Subject:",subject, stream, resent);
  322.    PutHead(NULL, "", stream, resent);  /* Terminate the header file   */
  323.  
  324. /*--------------------------------------------------------------------*/
  325. /*                    Copy the body of the message                    */
  326. /*--------------------------------------------------------------------*/
  327.  
  328.    while (fgets(buf, LSIZE, datain) != nil(char))
  329.    {
  330.       int result = fputs(buf, stream );
  331.       if (result == EOF)
  332.       {
  333.          printerr( pipename );
  334.          panic();
  335.       } /* if */
  336.       if (buf[strlen(buf)-1] != '\n')
  337.             fputc('\n', stream);
  338.    } /* while */
  339.  
  340.    if (!feof(datain))
  341.    {
  342.       printerr("Send_Mail:");
  343.       panic();
  344.    } /* if */
  345.  
  346.    if (datain != stdin)
  347.       fclose(datain);
  348.  
  349. /*--------------------------------------------------------------------*/
  350. /*    Append user's primary signature file, if autosign option on     */
  351. /*--------------------------------------------------------------------*/
  352.  
  353.    if ( bflag[F_AUTOSIGN] )
  354.       Append_Signature(stream, FALSE);
  355.  
  356.    fclose(stream);
  357.  
  358. /*--------------------------------------------------------------------*/
  359. /*                  Invoke the mail delivery program                  */
  360. /*--------------------------------------------------------------------*/
  361.  
  362.    if ( freopen( pipename, "r" , stdin) == NULL )
  363.    {
  364.       printerr(CONSOLE);
  365.       return FALSE;
  366.    }
  367.    else {
  368.       status = spawnlp( P_WAIT, RMAIL,RMAIL, "-t", NULL);
  369.       if ( status < 0 )
  370.       {
  371.          printerr( RMAIL );
  372.          printmsg(0,"Unable to execute rmail; mail not delivered.");
  373.       }
  374.       else if ( status > 0 )
  375.          printmsg(0,
  376.             "rmail returned non-zero status; delivery may be incomplete.");
  377.    } /* else */
  378.  
  379.    freopen( CONSOLE, "r", stdin);
  380.  
  381. /*--------------------------------------------------------------------*/
  382. /*               Log a copy of the mail for the sender                */
  383. /*--------------------------------------------------------------------*/
  384.  
  385.    if (bflag[F_SAVERESENT] || ! resent)
  386.       CopyOut(pipename);
  387.  
  388. /*--------------------------------------------------------------------*/
  389. /*                   Clean up and return to caller                    */
  390. /*--------------------------------------------------------------------*/
  391.  
  392.    remove(pipename);
  393.    free(pipename);
  394.    return (status == 0 );
  395.  
  396. } /*Send_Mail*/
  397.  
  398. /*--------------------------------------------------------------------*/
  399. /*    C o p y O u t                                                   */
  400. /*                                                                    */
  401. /*    Save copy of outgoing mail, if desired                          */
  402. /*--------------------------------------------------------------------*/
  403.  
  404. static void CopyOut( const char* input)
  405. {
  406.    FILE *datain;
  407.    FILE *dataout;
  408.    char buf[BUFSIZ];
  409.    char outbox[FILENAME_MAX];
  410.  
  411.    if (E_filesent == NULL)
  412.       return;
  413.  
  414.    strcpy( outbox, E_filesent);
  415.    expand_path( outbox, E_homedir, E_homedir, E_mailext );
  416.  
  417.    datain = FOPEN( input, "r", TEXT);
  418.  
  419.    if (datain == NULL )
  420.    {
  421.       printerr(input);
  422.       panic();
  423.    } /* if */
  424.  
  425.    dataout = FOPEN( outbox, "a", TEXT);
  426.    if (dataout == NULL )
  427.    {
  428.       printerr( outbox );
  429.       panic();
  430.    } /* if */
  431.  
  432.    fputs(MESSAGESEP,dataout);
  433.  
  434.    while (fgets(buf, BUFSIZ, datain) != nil(char))
  435.    {
  436.       int result = fputs(buf, dataout);
  437.       if (result == EOF)
  438.       {
  439.          printerr( outbox );
  440.          panic();
  441.       } /* if */
  442.    } /* while */
  443.  
  444.    if (!feof(datain))
  445.    {
  446.       printerr(input);
  447.       panic();
  448.    } /* if */
  449.  
  450.    fclose(datain);
  451.    fclose(dataout);
  452. } /* CopyOut */
  453.  
  454. /*--------------------------------------------------------------------*/
  455. /* P u t H e a d                                                      */
  456. /*                                                                    */
  457. /* Write one line of an RFC-822 header                                */
  458. /*--------------------------------------------------------------------*/
  459.  
  460.  static void PutHead( const char *label,
  461.                       const char *operand,
  462.                       FILE *stream,
  463.                       const boolean resent)
  464.  {
  465.    static boolean terminate = TRUE;
  466.                               /* If previous line was terminated     */
  467.  
  468.    if (label == NULL )        /* Terminate call?                     */
  469.    {                          /* Yes --> Reset Flag and return       */
  470.       fputc('\n', stream);    /* Terminate the current line          */
  471.       if (!resent)
  472.          fputc('\n', stream); /* Terminate the header file           */
  473.       terminate = TRUE;
  474.       return;
  475.    } /* if */
  476.  
  477.    if (strlen(label))         /* First line of a header?             */
  478.    {
  479.       if (!terminate)         /* Terminate previous line?            */
  480.          fputc('\n', stream);
  481.       if (resent)
  482.          fprintf(stream,"Resent-%s %s",label, operand);
  483.       else
  484.          fprintf(stream,"%-10s %s",label, operand);
  485.       terminate = FALSE;          /* Flag that we did not end file   */
  486.    } /* if */
  487.    else                       /* Continuing line                     */
  488.       fprintf(stream,",\n%-10s %s",label, operand);
  489.  } /* PutHead */
  490.  
  491. /*--------------------------------------------------------------------*/
  492. /*    C o l l e c t _ M a i l                                         */
  493. /*                                                                    */
  494. /*    Create mailbox file for delivery                                */
  495. /*--------------------------------------------------------------------*/
  496.  
  497. boolean Collect_Mail(FILE *stream,
  498.                   int argc,
  499.                   char **argv,
  500.                   const int current_msg,
  501.                   const boolean reply)
  502. {
  503.    boolean editonly = FALSE;
  504.    char Subuffer[LSIZE];
  505.    char *subject = NULL;
  506.    char *tmailbag;
  507.    int  c;                    /* Really a 'char', but who cares?     */
  508.    boolean done = FALSE;
  509.    FILE  *fmailbag;
  510.  
  511.  
  512. /*--------------------------------------------------------------------*/
  513. /*      Determine if we are running interactively; if not, just       */
  514. /*         pass the input stream to Send_Mail for processing          */
  515. /*--------------------------------------------------------------------*/
  516.  
  517.    if (!Is_Console(stream))
  518.    {
  519.       if ( equal(argv[0], "-s" ) )
  520.          return Send_Mail( stream, argc-2, argv+2, argv[1], FALSE);
  521.       else
  522.          return Send_Mail( stream , argc , argv, NULL , FALSE);
  523.    } /* if */
  524.  
  525. /*--------------------------------------------------------------------*/
  526. /*    We are running interactively; create a determine the name of    */
  527. /*                        our work file to be.                        */
  528. /*--------------------------------------------------------------------*/
  529.  
  530.    *Subuffer = '\0';          /* Assume no subject */
  531.    tmailbag = mktempname( NULL, "TXT");
  532.  
  533. /*--------------------------------------------------------------------*/
  534. /*         Determine if we should go straight into the editor         */
  535. /*--------------------------------------------------------------------*/
  536.  
  537.    editonly = bflag[F_AUTOEDIT] && (E_editor != NULL);
  538.  
  539.    if ( equal(argv[0],"-s"))     /* Any subject specified?           */
  540.    {
  541.       strcpy( Subuffer , argv[1] ); /* Save the subject for later    */
  542.       argv += 2;                 /* Skip over it in argument list    */
  543.       argc -= 2;                 /* Giving us fewer args left        */
  544.    }
  545.    else {                        /* No --> Prompt for one            */
  546.       if(Console_fgets(Subuffer,LSIZE,"Subject: "))
  547.       {
  548.          if (Subuffer[strlen(Subuffer) - 1 ] == '\n')
  549.             Subuffer[strlen(Subuffer)-1] = '\0';   /* End the subject */
  550.       }  /* If Console_fgets() */
  551.    } /* if ( equal(argv[0],"-s")) */
  552.  
  553. /*--------------------------------------------------------------------*/
  554. /*      Copy a message from the original input to temporary file      */
  555. /*--------------------------------------------------------------------*/
  556.  
  557.    fmailbag = FOPEN(tmailbag, "w", TEXT);
  558.    if (fmailbag == NULL )
  559.    {
  560.       printerr( tmailbag );
  561.       panic();
  562.    }
  563.  
  564. /*--------------------------------------------------------------------*/
  565. /*               Include incoming message if requested                */
  566. /*--------------------------------------------------------------------*/
  567.  
  568.    if ( bflag[F_AUTOINCLUDE] && reply)
  569.    {
  570.        CopyMsg(current_msg, fmailbag, fromheader , TRUE);
  571.        fprintf(stdout, "Message %d Included\n", current_msg+1);
  572.    } /* if ( bflag[F_AUTOINCLUDE] && reply) */
  573.  
  574. /*--------------------------------------------------------------------*/
  575. /*                     Edit the file if requested                     */
  576. /*--------------------------------------------------------------------*/
  577.  
  578.    if (editonly)              /* Enter editor immediately?     ahd   */
  579.    {                          /* Yes --> Go to it                    */
  580.       fclose(fmailbag);
  581.       Invoke_Editor(E_editor, tmailbag);
  582.    } /* if */
  583.    else {                     /* No  --> prompt for data       ahd   */
  584.       Prompt_Input( tmailbag , fmailbag , Subuffer, current_msg );
  585.       fclose(fmailbag);
  586.    } /*else */
  587.  
  588.  
  589.    do {
  590.       fputs("\nAbort, Continue, Edit, List, or Send? ",stdout);
  591.       c     = Get_One();             /* adaptation for QuickC */  /* pdm */
  592.       switch (tolower( c ))
  593.       {
  594.          case 'c':
  595.             puts("Continue");
  596.             fmailbag = FOPEN(tmailbag, "a", TEXT);
  597.             Prompt_Input( tmailbag , fmailbag , Subuffer, current_msg );
  598.             fclose(fmailbag);
  599.             break;
  600.  
  601.          case 'l':
  602.             puts("List");
  603.             Sub_Pager(tmailbag, islower(c) );
  604.             break;
  605.  
  606.          case 's':
  607.             puts("Send");
  608.             fmailbag = FOPEN(tmailbag, "r", TEXT);
  609.             if (fmailbag == NULL )
  610.             {
  611.                printerr(tmailbag);
  612.                panic();
  613.             }
  614.             if ( strlen( Subuffer ))
  615.                subject = Subuffer;
  616.             Send_Mail(fmailbag, argc, argv, subject, FALSE);
  617.             done = TRUE;
  618.             break;
  619.  
  620.          case 'e':
  621.             puts("Edit");
  622.             Invoke_Editor(E_editor, tmailbag);
  623.             break;
  624.  
  625.          case 'a':
  626.             fputs("Abort\nAre you sure? ", stdout);
  627.             safeflush();
  628.             c = Get_One();             /* for QuickC */          /* pdm */
  629.             switch (tolower(c)) {      /* unravel these two calls */
  630.             case 'y':
  631.                puts("Yes");
  632.                done = TRUE;
  633.                break;
  634.             default:
  635.                puts("No");
  636.             } /*switch*/
  637.             break;
  638.  
  639.          default:
  640.             puts("\n\aEnter A, C, E, L, or S.");
  641.             safeflush();
  642.             done = FALSE;
  643.       } /*switch*/
  644.    } while (!done);
  645.  
  646.    remove(tmailbag);
  647.    free(tmailbag);
  648.  
  649.    return TRUE;
  650. } /*Collect_Mail*/
  651.  
  652.  
  653. /*--------------------------------------------------------------------*/
  654. /*    P r o m p t _ I n p u t                                         */
  655. /*                                                                    */
  656. /*    Prompt for mail entry interactively.                            */
  657. /*--------------------------------------------------------------------*/
  658.  
  659. static void Prompt_Input( char *tmailbag,
  660.             FILE *fmailbag,
  661.             char *subject,
  662.             const int current_msg)
  663. {
  664.    char buf[LSIZE];
  665.  
  666.    printf("\nEnter message.  Enter ~? for help.  End input with %s\n",
  667.           bflag[ F_DOT ] ?  "a period (.)" :
  668.          "end-of-file (Control-Z)");
  669.    for ( ; ; )
  670.    {
  671.       if (Console_fgets(buf, LSIZE, "? "))
  672.       {
  673.          if (bflag[F_DOT] && equal(buf,".\n"))
  674.             break;
  675.          else if (Subcommand( buf, fmailbag, tmailbag, subject, current_msg) )
  676.             continue;      /*Don't write line out if subcommand   */
  677.       } /* if */
  678.       else
  679.          break;            /* Exit loop if end of file            */
  680.  
  681.       if (fputs(buf, fmailbag) == EOF )
  682.       {
  683.          printerr( tmailbag );
  684.          panic();
  685.       } /* if (fputs(buf, fmailbag) == EOF ) */
  686.  
  687.       if (buf[strlen(buf)-1] != '\n')
  688.          fputc('\n', fmailbag);
  689.    } /* for */
  690. } /* Prompt_Input */
  691.  
  692. /*--------------------------------------------------------------------*/
  693. /*    S u b c o m m a n d                                             */
  694. /*                                                                    */
  695. /*    Handle tilde (~) subcommands for Interactive mail               */
  696. /*--------------------------------------------------------------------*/
  697.  
  698. static boolean Subcommand( char *buf,
  699.                            FILE *fmailbag,
  700.                            char *tmailbag,
  701.                            char *subject,
  702.                            const int current_msg)
  703. {
  704.    int message;
  705.    char fname[FILENAME_MAX];
  706.    char *token;
  707.    FILE *stream;
  708.  
  709.    if(*buf == '~')        /* Handle mail subcommands  pdm */
  710.    {
  711.       switch(buf[1])
  712.       {
  713. /*--------------------------------------------------------------------*/
  714. /*              Put signature file into current message               */
  715. /*--------------------------------------------------------------------*/
  716.  
  717.          case 'a':
  718.          case 'A':
  719.             Append_Signature(fmailbag, isupper( buf[1] ));
  720.             fputs("(continue)\n", stdout);
  721.             break;
  722.  
  723. /*--------------------------------------------------------------------*/
  724. /*                       Edit outgoing message                        */
  725. /*--------------------------------------------------------------------*/
  726.  
  727.          case 'v':            /* UNIX allows 'v'isual editor         */
  728.          case 'e':
  729.             /* invoke editor with current msg */
  730.             fclose(fmailbag);
  731.             Invoke_Editor(E_editor, tmailbag);
  732.             fmailbag = FOPEN(tmailbag, "a", TEXT);
  733.             fputs("(continue)\n", stdout);
  734.             break;
  735.  
  736. /*--------------------------------------------------------------------*/
  737. /*                 Include any letter in this message                 */
  738. /*--------------------------------------------------------------------*/
  739.  
  740.          case 'f':
  741.          case 'F':
  742.          case 'i':
  743.          case 'I':
  744.          case 'm':
  745.          case 'M':
  746.             if (fmailbox == NULL)
  747.                puts("Mailbox not accessible!");
  748.             else {
  749.                int *item_list;
  750.                int next_item = PushItemList( &item_list );
  751.                boolean first_pass = TRUE;
  752.  
  753.                token = GetString( &buf[2] );
  754.  
  755.                if (SelectItems( &token, current_msg, LETTER_OP ))
  756.                while( Get_Operand( &message, &token, LETTER_OP, first_pass))
  757.                {
  758.                   CopyMsg( message , fmailbag,
  759.                            islower(buf[1]) ? fromheader : noseperator ,
  760.                            tolower(buf[1]) != 'f');
  761.                   fprintf(stdout, "Message %d included\n", message + 1);
  762.                   first_pass = FALSE;
  763.                } /* while */
  764.  
  765.                PopItemList( item_list, next_item );
  766.             } /* else */
  767.             break;
  768.  
  769. /*--------------------------------------------------------------------*/
  770. /*                       Print current message                        */
  771. /*--------------------------------------------------------------------*/
  772.  
  773.          case 'p':
  774.          case 'P':
  775.             fclose(fmailbag);
  776.             Sub_Pager(tmailbag, islower(buf[1]) );
  777.             fmailbag = FOPEN(tmailbag, "a", TEXT);
  778.             fputs("(continue)\n", stdout);
  779.             break;
  780.  
  781. /*--------------------------------------------------------------------*/
  782. /*                           Include a file                           */
  783. /*--------------------------------------------------------------------*/
  784.  
  785.          case 'r':
  786.             token = strtok( &buf[2], " \t\n");
  787.             if ( token == NULL )
  788.             {
  789.                printf("Need a file name for this command!\n");
  790.                break;
  791.             }
  792.             strcpy( fname, token );
  793.             if ( expand_path( fname, NULL, E_homedir , NULL) == NULL )
  794.                break;
  795.             stream = FOPEN( fname, "r", TEXT);
  796.             if (stream == NULL )
  797.                printerr(fname);
  798.             else while( fgets( buf, LSIZE, stream ))
  799.             {
  800.                fputs( buf, fmailbag);
  801.                if ferror( fmailbag )
  802.                {
  803.                   printerr( tmailbag);
  804.                   break;
  805.                } /* if */
  806.             } /* else while */
  807.             if (ferror( stream ) )
  808.             {
  809.                printerr( fname );
  810.                clearerr( stream );
  811.             } /* if */
  812.             fclose( stream );
  813.             fputs("(continue)\n", stdout);
  814.             break;
  815.  
  816.  
  817. /*--------------------------------------------------------------------*/
  818. /*                        Change mail subject                         */
  819. /*--------------------------------------------------------------------*/
  820.  
  821.          case 's':
  822.             token = GetString( &buf[2] );
  823.             if ( token != NULL )
  824.             {
  825.                strcpy( subject, token );
  826.             }
  827.             else
  828.                printf("No new subject, command ignored\n");
  829.             printf("Subject: %s\n",subject);
  830.             break;
  831.  
  832. /*--------------------------------------------------------------------*/
  833. /*                                Help                                */
  834. /*--------------------------------------------------------------------*/
  835.  
  836.          case '?':
  837.          {
  838.             mkfilename(fname, E_confdir, "tilde.hlp");
  839.             Sub_Pager( fname, TRUE );
  840.             break;
  841.          }
  842.  
  843. /*--------------------------------------------------------------------*/
  844. /*                             A subshell                             */
  845. /*--------------------------------------------------------------------*/
  846.  
  847.          case '!':
  848.             token = strtok( &buf[2], "\n");
  849.             subshell( token );
  850.             break;
  851.  
  852. /*--------------------------------------------------------------------*/
  853. /*                     Pipe mail through a filter                     */
  854. /*--------------------------------------------------------------------*/
  855.  
  856.          case '|':
  857.             fclose( fmailbag );
  858.             filter( tmailbag, &buf[2] );
  859.             fmailbag = FOPEN(tmailbag, "a", TEXT);
  860.             fputs("(continue)\n", stdout);
  861.             break;
  862.  
  863. /*--------------------------------------------------------------------*/
  864. /*                          Invalid command                           */
  865. /*--------------------------------------------------------------------*/
  866.  
  867.          default:
  868.             fputs("Unknown mail subcommand, ~? for help.\n",
  869.                   stdout);
  870.             break;
  871.       } /* switch */
  872.  
  873.       return TRUE;
  874.    } /* if */
  875.    else
  876.       return FALSE;           /* It wasn't a sub-command             */
  877. } /*SubCommand*/
  878.  
  879. /*--------------------------------------------------------------------*/
  880. /*    f i l t e r                                                     */
  881. /*                                                                    */
  882. /*    Filter the next of an outgoing program into the output mail     */
  883. /*--------------------------------------------------------------------*/
  884.  
  885. static void filter( char *tmailbag, char *command)
  886. {
  887.    char pipename[FILENAME_MAX];
  888.    char *argv[50];
  889.    struct stat statbuf;
  890.    int    argc;
  891.    int    result = 0;
  892.  
  893. /*--------------------------------------------------------------------*/
  894. /*          Break the command to execute down into arguments          */
  895. /*--------------------------------------------------------------------*/
  896.  
  897.    argc = getargs(command, argv);
  898.  
  899.    if ( argc == 0 )
  900.    {
  901.       printf("No filter name given!\n");
  902.       return;
  903.    }
  904.    argv[argc] = NULL;
  905.  
  906. /*--------------------------------------------------------------------*/
  907. /*   Set up our standard input and standard output for the command    */
  908. /*--------------------------------------------------------------------*/
  909.  
  910.    mktempname(pipename, "TXT");
  911.  
  912.    if ( freopen(tmailbag, "r", stdin) == NULL )
  913.       printerr( tmailbag );
  914.    else if (freopen(pipename, "w", stdout) == NULL )
  915.    {
  916.       printerr( pipename );
  917.       freopen("con", "r", stdin);
  918.    }
  919. /*--------------------------------------------------------------------*/
  920. /*                          Run the command                           */
  921. /*--------------------------------------------------------------------*/
  922.    else {
  923.       result = spawnvp( P_WAIT, argv[0], argv );
  924.       freopen("con", "w", stdout);
  925.       setbuf(stdout, NULL );
  926.       freopen("con", "r", stdin);
  927.       setbuf(stdin, NULL );
  928.  
  929.       if (result == -1)       /* Did spawn fail?            */
  930.          printerr(argv[0]);   /* Yes --> Report error       */
  931.       else if( stat( pipename, &statbuf) <0 )   /* Create output?    */
  932.       {
  933.          printf(0,"Cannot determine status of output %s",pipename);
  934.          printerr( pipename );
  935.       }
  936.       else if( statbuf.st_size == 0 )  /* Anything in the file?      */
  937.          printf("Outfile file is empty!\n");
  938.       else {                  /* Good output, replace input file     */
  939.          remove( tmailbag );
  940.          if (rename( pipename, tmailbag ))
  941.             printerr( pipename );
  942.       } /* else */
  943.    } /* else */
  944.  
  945. /*--------------------------------------------------------------------*/
  946. /*                   Clean up and return to caller                    */
  947. /*--------------------------------------------------------------------*/
  948.  
  949.    remove( pipename );
  950.  
  951. } /* filter */
  952.  
  953. /*--------------------------------------------------------------------*/
  954. /*    G e t S t r i n g                                               */
  955. /*                                                                    */
  956. /*    Get non-whitespace in a string                                  */
  957. /*--------------------------------------------------------------------*/
  958.  
  959. static char *GetString( char *input)
  960. {
  961.    char *end;
  962.  
  963. /*--------------------------------------------------------------------*/
  964. /*                   Look for first data in string                    */
  965. /*--------------------------------------------------------------------*/
  966.  
  967.    while( isspace( *input ))
  968.       input++ ;
  969.  
  970. /*--------------------------------------------------------------------*/
  971. /*   If no input or all blanks, return NULL to denote empty string    */
  972. /*--------------------------------------------------------------------*/
  973.  
  974.    if (*input == '\0')
  975.       return NULL ;
  976.  
  977. /*--------------------------------------------------------------------*/
  978. /*                Delete whitespace from end of string                */
  979. /*--------------------------------------------------------------------*/
  980.  
  981.    end = &input[ strlen( input ) - 1 ];
  982.  
  983.    while (isspace(*end))
  984.       *end-- = '\0';
  985.  
  986. /*--------------------------------------------------------------------*/
  987. /*                 Return beginning of string to caller               */
  988. /*--------------------------------------------------------------------*/
  989.  
  990.    return input;
  991.  
  992. } /* GetString */
  993.